<?php

class cntrlmanagegroup implements viewController {

	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js", "manageGroup.js"), "CSS" => array("facebox.css"));
	private $pageName = "";
	private $userObj;
	private $thisID;
	
	public function __construct(user &$userObj){
		if( $userObj->getAccessLevel() == -1 ){
			// User has to be a global admin to access this view
			header("Location: home");
			exit;
		}
			
		$this->side = new template('control.manageGroup');
		$this->area = new template('area.manageGroup');
		
		parse_str( $_GET['act'] );
		
		if($id == "") {
			header("Location: home");
			exit;
		}
		
		$id = (int) $id;
		$this->thisID = $id;
		// Do we have access permissions to this
		$groups = $userObj->getManagedGroups();
		
		$userAdmin = false;
		
		foreach($groups as $group){
			if($group[0] == $id) $userAdmin = true;
		}
		
		if($userAdmin == false){
			header("Location: home");
			exit;
		}
		
		$this->userObj = $userObj;
		$this->renderPage($id);
		$this->renderGroupList($groups);
	}

	private function renderPage($id){
		$group = group::getGroupInfo($id);
		$requests = $this->userObj->getAuthorisationRequests();
		
		
		$this->area->replaceAll(array(
			"groupName" => $group['name'],
			"groupDescription" => $group['description'],
			"gID" => $group['gID']
		));

		if((BOOL) $group['byInvitation'] ){
			$this->area->replace("addUserButton", "<button id='addUsers'><var id='lang_354_inviteUsers'></var></button><br />");
			$this->area->replace('userTable', "");	
		} else {
			$this->area->replace("addUserButton", "");
			
			// Build the table.
			$table = "";
			
			foreach($requests as $groupRequests){
				foreach($groupRequests['pending'] as $userRequest){
					
					$tick = "<a href='#' class='acceptUser' id='a" . $userRequest['user_uID'] . "'><input type='hidden' value='" . $userRequest['user_uID'] . "' name='userID' /><input type='hidden' value='" . $groupRequests['gID'] . "' name='groupID' /><img src='" . SYS_BASEURL . "images/tick-button.png' /></a>";
					$cross = "<a href='#' class='denyUser' id='d" . $userRequest['user_uID'] . "'><input type='hidden' value='" . $userRequest['user_uID'] . "' name='userID' /><input type='hidden' value='" . $groupRequests['gID'] . "' name='groupID' /><img src='" . SYS_BASEURL . "images/cross-button.png' /></a>";	
					
					$str = "<tr>";
						$str .= "<td>" . $groupRequests['groupName'] .  "</td>";				
						$str .= "<td>" . $groupRequests['institutionName'] .  "</td>";	
						$str .= "<td>" . $userRequest['firstName'] . "</td>";	
						$str .= "<td>" . $userRequest['lastName'] . "</td>";	
						$str .= "<td>" . $userRequest['userName'] . "</td>";	
						$str .= "<td style='text-align:center'>" . $tick . "</td>";	
						$str .= "<td style='text-align:center'>" . $cross . "</td>";
					$str .= "</tr>";
					
					$table .= $str . "\n";
					$str = "";			
				}
			}			
			
			$this->area->replace('userTable', $table);	
		}
		
		
		
		
	}

	protected function renderGroupList($groups){
		$output = "";
		
		foreach($groups as $managedGroups){
			if($this->thisID == $managedGroups[0]){
				$output .= "<option value='" . $managedGroups[0] . "' selected='selected'>" . $managedGroups[1] . "</option>\n";
			} else {
				$output .= "<option value='" . $managedGroups[0] . "'>" . $managedGroups[1] . "</option>\n";
			}
		}
		
		$this->side->replace('groupList', $output);
	}

	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return "";
	}

}

?>